<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: SmsTemplateMap.php
// +----------------------------------------------------------------------
// | Author: liuju
// +----------------------------------------------------------------------
// | Description: 以下为短信模板对应信息 所有参数使用string类型 必须使用（string）或者（strval）来转换,不然会有验签错误
// +----------------------------------------------------------------------
// | Others:  验证码映射关系
// +----------------------------------------------------------------------
// | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
// +----------------------------------------------------------------------
// | 1.Date: 2019-01-10 Author: liuju Modification: 创建
// +----------------------------------------------------------------------

namespace App\Constant;


use App\Events\SmsSendEvent;
use App\Exceptions\GeneralException;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;

class SmsTemplate
{


    /**
     * 注册短信模板id
     */
    const  TEMPLATE_REGISTER = '71';

    /**
     * 重置密码模板
     */
    const TEMPLATE_RESET = '74';


    /**
     * 改密码
     */
    const TEMPLATE_MODIFY_PASS = '70';

    /**
     * 验证手机
     */
    const TEMPLATE_VERIFY_PHONE = '79';

    /**
     * 融之家注册成功
     */
    const TEMPLATE_RZG_REGISTER = '81';

    /**
     * 绑卡
     */
    const TEMPLATE_BIND_BANK_CARD = '72';

    /**
     * 未通过风控
     */
    const TEMPLATE_NOTICE_NOT_PASS_RISK = '76';

    /**
     * 风控审批成功
     */
    const TEMPLATE_NOTICE_PASS_RISK = '82';


    /**
     * 确认要款成功--晋商
     */
    const TEMPLATE_CONFIRM_LOAN_APPLY_SUCCESS = '84';


    /**
     * 确认要款失败
     */
    const TEMPLATE_CONFIRM_LOAN_APPLY_FAIL = '86';

    /**
     * 放款成功
     */
    const TEMPLATE_LOAN_SUCCESS = '78';

    /**
     * 晋商渠道放款成功
     */
    const TEMPLATE_LOAN_SUCCESS_BY_GD= '83';

    /**
     * 放款失败切换渠道
     */
    const TEMPLATE_LOAN_FAIL_CHANGE_CHANNEL = '85';

    /**
     * 扣款提醒
     */
    const TEMPLATE_NOTICE_DEDUCT_MONEY = '75';

    /**
     * 还款当天提醒
     */
    const TEMPLATE_REPLY_DAY = '';

    /**
     * 自动还款成功
     */
    const TEMPLATE_AUTOMATIC_REPLY_SUCCESS = '99';

    /**
     * 自动还款失败
     */
    const TEMPLATE_AUTOMATIC_REPLY_FAIL = '98';

    /**
     * 手动还款成功
     */
    const TEMPLATE_HANDLE_REPLAY_SUCCESS = '91';

    /**
     * 手动还款失败
     */
    const TEMPLATE_HANDLE_REPLY_FAIL = '92';

    /**
     * 提前结清成功
     */

    const TEMPLATE_EARLY_SETTLEMENT_SUCCESS = '93';

    /**
     * 提前结清失败
     */
    const TEMPLATE_EARLY_SETTLEMENT_FAIL = '94';

    /**
     * 还款逾期
     */
    const TEMPLATE_REPLY_OVERDUE = '95';

    /**
     * 还款超时1天
     */
    const TEMPLATE_REPLY_OVERDUE_ONE_DAY = '96';
    /**
     * 还款超时3天
     */
    const TEMPLATE_REPLY_OVERDUE_THREE_DAY = '90';

    /**
     * 晋商还款提醒
     */
    const TEMPLATE_REPLY_NOTICE_BY_JS = '';

    /**
     * 还款到期提醒(光大渠道)
     */
    const TEMPLATE_REPLY_ON_DATE_BY_BG = '';

    /**
     * 用户生日
     */
    const TEMPLATE_SMS_USER_BIRTHDAY = '132';

    /**
     * 注册短信验证码详情
     */
    const  SMS_REGISTER = [
        'templateId' => self::TEMPLATE_REGISTER,
        'messageParamMap' => [
            'validCode' => '', // 验证码内容
        ],
    ];


    /**
     * 重置密码验证码
     */
    const  SMS_RESET = [
        'templateId' => self::TEMPLATE_RESET,
        'messageParamMap' => [
            'validCode' => '',  //验证码内容
        ],
    ];


    /**
     * 登录验证码修改
     */
    const SMS_MODIFY_PASS = [
        'templateId' => self::TEMPLATE_MODIFY_PASS,
        'messageParamMap' => [
            'validCode' => '',   //验证码内容
        ],
    ];



    /**
     * 绑定手机号
     */
    const SMS_VERIFY_PHONE = [
        'templateId' => self::TEMPLATE_VERIFY_PHONE,
        'messageParamMap' => [
            'validCode' => '',  //验证码内容
        ],
    ];


    /**
     * 融之家注册成功提醒
     */
    const SMS_RZG_REGISTER = [
        'templateId' => self::TEMPLATE_RZG_REGISTER,
        'messageParamMap' => [
            'name' => '',  //用户名
            'password' => '', //密码
        ],
    ];


    /**
     * 绑定银行卡
     */
    const  SMS_BIND_BANK_CARD = [
        'templateId' => self::TEMPLATE_BIND_BANK_CARD,
        'messageParamMap' => [
            'validCode' => '',  //验证码内容
        ],
    ];


    /**
     * 未通过风控提醒
     */
    const SMS_NOTICE_NOT_PASS_RISK = [
        'templateId' => self::TEMPLATE_NOTICE_NOT_PASS_RISK,
        'messageParamMap' => [
            'name' => '', //用户姓名
            'amount' => '', //借款金额
            'term' => '', // 分期数
        ],
    ];

    /**
     * 审批成功要款提醒
     */
    const SMS_NOTICE_PASS_RISK = [
        'templateId' => self::TEMPLATE_NOTICE_PASS_RISK,
        'messageParamMap' => [
            'name' => '', //用户姓名
//            'amount' => '', //借款金额
//            'term' => '', // 分期数
        ],
    ];

    /**
     * 确认要款成功-晋商
     */
    const SMS_CONFIRM_LOAN_APPLY_SUCCESS = [
        'templateId' => self::TEMPLATE_CONFIRM_LOAN_APPLY_SUCCESS,
        'messageParamMap' => [
            'name' => '', //用户姓名
        ],
    ];

    /**
     * 确认要款失败
     */
    const SMS_CONFIRM_LOAN_APPLY_FAIL = [
        'templateId' => self::TEMPLATE_CONFIRM_LOAN_APPLY_FAIL,
        'messageParamMap' => [
            'name' => '', //用户姓名
            'amount' => '', //总借款金额
            'term' => '', // 分期数
        ],
    ];


    /**
     * 放款成功
     */
    const SMS_LOAN_SUCCESS = [
        'templateId' => self::TEMPLATE_LOAN_SUCCESS,
        'messageParamMap' => [
            'name' => '', //用户姓名
            'amount' => '', //放款金额
        ],
    ];

    /**
     * 放款成功(光大渠道GD)
     */
    const SMS_LOAN_SUCCESS_BY_GD = [
        'templateId' => self::TEMPLATE_LOAN_SUCCESS_BY_GD,
        'messageParamMap' => [
            'name' => '', //用户姓名
            'amount' => '', //放款金额
        ],
    ];

    /**
     * 放款失败切换渠道
     */
    const SMS_LOAN_FAIL_CHANGE_CHANNEL = [
        'templateId' => self::TEMPLATE_LOAN_FAIL_CHANGE_CHANNEL,
        'messageParamMap' => [
            'name' => '', //用户姓名
            'amount' => '', //总借款金额
            'term' => '', // 分期数
        ],
    ];


    /**
     * 扣款提醒
     */
    const SMS_NOTICE_DEDUCT_MONEY = [
        'templateId' => self::TEMPLATE_NOTICE_DEDUCT_MONEY,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'eachAmount' => '', //本期应还金额
            'month' => '', //月份
            'day' => '', //天
            'backCard' => '', //银行卡尾号
        ],
    ];

    /**
     * 还款当天提醒
     */
    const SMS_REPLY_DAY = [
        'templateId' => self::TEMPLATE_REPLY_DAY,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'eachAmount' => '', //本期应还金额
            'backCard' => '', //还款银行卡尾号
        ],
    ];


    /**
     * 自动还款成功
     */
    const SMS_AUTOMATIC_REPLY_SUCCESS = [
        'templateId' => self::TEMPLATE_AUTOMATIC_REPLY_SUCCESS,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'eachAmount' => '', //本期应还金额
            'term' => '', //当前期数
        ],
    ];

    /**
     * 自动还款失败
     */
    const SMS_AUTOMATIC_REPLY_FAIL = [
        'templateId' => self::TEMPLATE_AUTOMATIC_REPLY_FAIL,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'cardNo' => '', //还款银行卡尾号
        ],
    ];

    /**
     * 手动期还款成功
     */
    const SMS_HANDLE_REPLAY_SUCCESS = [
        'templateId' => self::TEMPLATE_HANDLE_REPLAY_SUCCESS,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'eachAmount' => '', //本期应还金额
            'term' => '', //当前期数
        ],
    ];

    /**
     * 手动还款失败
     */
    const SMS_HANDLE_REPLY_FAIL = [
        'templateId' => self::TEMPLATE_HANDLE_REPLY_FAIL,
        'messageParamMap' => [
            'name' => '',  //用户姓名
        ],
    ];

    /**
     * 提前结清成功
     */
    const SMS_EARLY_SETTLEMENT_SUCCESS = [
        'templateId' => self::TEMPLATE_EARLY_SETTLEMENT_SUCCESS,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'amount' => '', //金额
            'term' => '', //期数
        ],
    ];

    /**
     * 提前结清失败
     */

    const SMS_EARLY_SETTLEMENT_FAIL = [
        'templateId' => self::TEMPLATE_EARLY_SETTLEMENT_FAIL,
        'messageParamMap' => [
            'name' => '',  //用户姓名
        ],
    ];

    /**
     * 还款逾期
     */
    const SMS_REPLY_OVERDUE = [
        'templateId' => self::TEMPLATE_REPLY_OVERDUE,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'month' => '', //月份
            'day' => '', //天
        ],
    ];

    /**
     * 还款超时一天
     */
    const SMS_REPLY_OVERDUE_ONE_DAY = [
        'templateId' => self::TEMPLATE_REPLY_OVERDUE_ONE_DAY,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'month' => '', //月份
            'day' => '', //天
        ],
    ];

    /**
     * 还款超时三天
     */
    const SMS_REPLY_OVERDUE_THREE_DAY = [
        'templateId' => self::TEMPLATE_REPLY_OVERDUE_THREE_DAY,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'month' => '', //月份
            'day' => '', //天
        ],
    ];

    /**
     * 还款提醒-晋商
     */
    const SMS_REPLY_NOTICE_BY_JS = [
        'templateId' => self::TEMPLATE_REPLY_NOTICE_BY_JS,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'eachAmount' => '', //本期应还金额
            'month' => '', //月份
            'day' => '', //天
            'backCard' => '', //还款银行卡尾号
        ],
    ];

    /**
     * 还款到期提醒(光大渠道)
     */

    const SMS_REPLY_ON_DATE_BY_BG = [
        'templateId' => self::TEMPLATE_REPLY_ON_DATE_BY_BG,
        'messageParamMap' => [
            'name' => '',  //用户姓名
            'eachAmount' => '', //本期应还金额
            'month' => '', //月份
            'day' => '', //天
            'backCard' => '', //还款银行卡尾号
        ],
    ];

    /**
     * 用户生日
     */
    const SMS_USER_BIRTHDAY = [
        'templateId' => self::TEMPLATE_SMS_USER_BIRTHDAY,
    ];

    public function __construct()
    {

    }


    /**
     * Description:
     * Author: liuju
     * DateTime: 2019-01-15 14:07
     * @param $templateId
     * @param $phone
     * @param array $message
     * @throws GeneralException
     * @throws \ReflectionException
     */
    public static function send($templateId, $phone, $message = [])
    {

        if(!$templateId){
           return false;
        }
        $reflection = new \ReflectionClass(self::class);
        $params = [];
        foreach ($reflection->getConstants() as $item) {
            if (!empty($item['templateId']) && $item['templateId'] == $templateId) {
                $params = $item;
                break;
            }
        }
        if (!$params) {
            Log::debug('「调用短信模板id（'.$templateId.'）找不到对应的短信模板Id，请检查对应配置」');
            throw  new GeneralException('找不到对应的短信模板Id，请检查对应配置');
        }
        if (!empty($params['messageParamMap'])) {
            $diff = array_diff(array_keys($params['messageParamMap']), array_keys($message));
            if ($diff) {
                Log::debug('「调用短信模板id（'.$templateId.'）短信参数列表Message中缺少'. implode('、', $diff) . '字段」');
                throw   new  GeneralException('短信参数列表Message中缺少' . implode('、', $diff) . '字段');
            } else {
                foreach($message  as $key=>$val){
                    $message[$key] = strval($val);
                }
                if (is_array($phone)) {
                    $phone = array_map(function ($val) {
                        return strval($val);
                    }, $phone);
                } else {
                    $phone = [strval($phone)];
                }
                $params['messageParamMap'] = $message;
            }
        }
        event(new SmsSendEvent($params, $phone));
    }
}